<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ include file="/WEB-INF/jsp/component/common.jsp" %>
<!DOCTYPE html>
<html>
<head>
    <title>项目参考值修改</title>
    <meta charset="utf-8">
    <!-- Bootstrap Core CSS -->
    <link href="${css}/bootstrap.min.css" rel="stylesheet"/>
    <!-- bootstrapValidator验证 -->
    <link href="${assets}/bootstrapValidator/css/bootstrapValidator.css" rel="stylesheet"/>
</head>
<body style="width: 98%">
<form id="myForm" class="form-horizontal" method="post" >
    <input type="hidden" name="id" value="${testProject.id}">
    <div class="form-group">
        <label for="kind" class="col-sm-3 control-label">项目大项</label>
        <div class="col-sm-9">
            <select id="kind" name="kind" class="form-control">
                <option value="">请选择...</option>
                <c:forEach items="${kindList}" var="kind">
                    <option value="${kind.testKindCode}" <c:if test='${kind.testKindCode==testProject.testKindCode}'>selected="selected"</c:if> >${kind.testKindName}</option>
                </c:forEach>
            </select>
        </div>
    </div>
    <div class="form-group">
        <label for="item" class="col-sm-3 control-label">项目子项</label>
        <div class="col-sm-9">
            <select id="item" name="testItemCode" class="form-control">
                <option value="">请选择...</option>
                <c:forEach items="${itemList}" var="item">
                    <option value="${item.testItemCode}" <c:if test='${item.testItemCode==testProject.testItemCode}'>selected="selected"</c:if> >${item.testItemName}</option>
                </c:forEach>
            </select>
        </div>
    </div>
    <div class="form-group">
        <label for="unit" class="col-sm-3 control-label">单位</label>
        <div class="col-sm-9">
            <input type="text" class="form-control" id="unit" name="unit" value="${testProject.unit}"
                   placeholder="请输入单位">
        </div>
    </div>
    <div class="form-group">
        <label for="minAge" class="col-sm-3 control-label">年龄范围（男）</label>
        <div class="col-sm-4">
            <input type="text" class="form-control" id="minAge" name="minAge" value="${testProject.minAge}"
                   placeholder="请输入最小年龄值">
        </div>
        <div class="col-sm-1">
            --
        </div>
        <div class="col-sm-4">
            <input type="text" class="form-control" id="maxAge" name="maxAge" value="${testProject.maxAge}"
                   placeholder="请输入最大年龄值">
        </div>
    </div>
    <div class="form-group">
        <label for="testMinValue" class="col-sm-3 control-label">参考值范围（男）</label>
        <div class="col-sm-4">
            <input type="text" class="form-control" id="testMinValue" name="testMinValue" value="${testProject.testMinValue}"
                   placeholder="请输入最小参考值">
        </div>
        <div class="col-sm-1">
            --
        </div>
        <div class="col-sm-4">
            <input type="text" class="form-control" id="testMaxValue" name="testMaxValue" value="${testProject.testMaxValue}"
                   placeholder="请输入最大参考值">
        </div>
    </div>
    <div class="form-group">
        <label for="minAgeGirl" class="col-sm-3 control-label">年龄范围（女）</label>
        <div class="col-sm-4">
            <input type="text" class="form-control" id="minAgeGirl" name="minAgeGirl" value="${testProject.minAgeGirl}"
                   placeholder="请输入最小年龄值">
        </div>
        <div class="col-sm-1">
            --
        </div>
        <div class="col-sm-4">
            <input type="text" class="form-control" id="maxAgeGirl" name="maxAgeGirl" value="${testProject.maxAgeGirl}"
                   placeholder="请输入最大年龄值">
        </div>
    </div>
    <div class="form-group">
        <label for="testMinValueGirl" class="col-sm-3 control-label">参考值范围（女）</label>
        <div class="col-sm-4">
            <input type="text" class="form-control" id="testMinValueGirl" name="testMinValueGirl" value="${testProject.testMinValueGirl}"
                   placeholder="请输入最小参考值">
        </div>
        <div class="col-sm-1">
            --
        </div>
        <div class="col-sm-4">
            <input type="text" class="form-control" id="testMaxValueGirl" name="testMaxValueGirl" value="${testProject.testMaxValueGirl}"
                   placeholder="请输入最大参考值">
        </div>
    </div>
    <div class="modal-footer" style="text-align: center ">
        <button type="button" class="btn btn-default" data-dismiss="modal" id="closeModalButt">关闭</button>
        <button type="submit" class="btn btn-primary">保存</button>
    </div>
</form>

<!-- jQuery -->
<script src="${js}/jquery-1.9.1.min.js"></script>
<!-- Bootstrap Core JavaScript -->
<script src="${js}/bootstrap.min.js"></script>

<script src="${js}/bootbox.js"></script>
<!-- bootstrapValidator验证 -->
<script src="${assets}/bootstrapValidator/js/bootstrapValidator.js"></script>
<!-- bootstrapValidator验证 国际化中文 -->
<script src="${assets}/bootstrapValidator/js/language/zh_CN.js"></script>

<script type="text/javascript">

    $(function () {
        var itemData = [];
        <c:forEach items="${itemList}" var="item">
            itemData.push({testItemCode:"${item.testItemCode}", testItemName:"${item.testItemName}", testUnit:"${item.testUnit}"});
        </c:forEach>
        $("#kind").change(function(){
            $("#unit").val("");
            if(this.value==""){
                var objs = document.getElementById("item");
                objs.options.length = 1;
            }else{
                $.ajax({
                    url:'${ctx}/testProject/getItemList',
                    type:'post',
                    data:{kindCode:this.value},
                    async : false, //默认为true 异步
                    error:function(){
                        alert('error');
                    },
                    success:function(data){
                        var objs = document.getElementById("item");
                        objs.options.length = 1;
                        var item = data.data;
                        itemData = [];
                        for(var i=0;i<item.length;i++){
                            //保存项目子项数据到itemData中
                            itemData.push(item[i]);
                            //设置项目子项下拉框选项
                            var childOption = document.createElement("option");
                            childOption.value = item[i].testItemCode;
                            childOption.innerHTML = item[i].testItemName;
                            objs.appendChild(childOption);
                        }
                    }
                });
            }
        });

        $("#item").change(function(){
            if(this.value==""){
                $("#unit").val("");
            }else{
                for(var i=0;i<itemData.length;i++){
                    if(this.value==itemData[i].testItemCode){
                        $("#unit").val(itemData[i].testUnit);
                    }
                }
            }
        });

        $('#myForm').bootstrapValidator({
            message: 'This value is not valid',
            feedbackIcons: {
                valid: 'glyphicon glyphicon-ok',
                invalid: 'glyphicon glyphicon-remove',
                validating: 'glyphicon glyphicon-refresh'
            },
            fields: {
                kind: {
                    group: '.col-sm-9',
                    validators: {
                        notEmpty: true
                    }
                },
                testItemCode: {
                    group: '.col-sm-9',
                    validators: {
                        notEmpty: true
                    }
                },
                minAge: {
                    group: '.col-sm-4',
                    verbose: false,
                    validators: {
                        notEmpty: true,
                        digits: true,
                        callback: {
                            message:"请填写小于“年龄范围（男）最大值”的值",
                            callback: function(value, validator) {
                                var maxAge = validator.getFieldElements('maxAge').val();
                                if(Number(maxAge)>=Number(value)){
                                    validator.updateStatus('maxAge', 'VALID');
                                    //验证项目子项是否为选择
                                    validator.resetField("kind").validateField("kind");
                                    validator.resetField("testItemCode").validateField("testItemCode");
                                    if(!validator.isValidField("testItemCode")){
                                        return {
                                            valid: false,
                                            message: '项目子项不能为空'
                                        };
                                    }else{
                                        return true;
                                    }
                                }else{
                                    return {
                                        valid: false,
                                        message: '请填写小于“年龄范围（男）最大值”的值'
                                    };
                                }
                            }
                        },
                        remote: {
                            type: "post",
                            delay: 500,
                            url: '${ctx}/testProject/validateAgeRange',
                            data: function (validator) {
                                return {
                                    minAge: $("#minAge").val(),
                                    maxAge: $("#maxAge").val(),
                                    testItemCode: $("#item").val(),
                                    exception: '${testProject.id}'
                                };
                            },
                            message: '该年龄段已经存在参考值'
                        },
                    }
                },
                maxAge: {
                    group: '.col-sm-4',
                    verbose: false,
                    validators: {
                        notEmpty: true,
                        digits: true,
                        callback: {
                            callback: function(value, validator) {
                                var minAge = validator.getFieldElements('minAge').val();
                                if(Number(value)>=Number(minAge)){
                                    validator.updateStatus('minAge', 'VALID');
                                    //验证项目子项是否为选择
                                    validator.resetField("kind").validateField("kind");
                                    validator.resetField("testItemCode").validateField("testItemCode");
                                    if(!validator.isValidField("testItemCode")){
                                        return {
                                            valid: false,
                                            message: '项目子项不能为空'
                                        };
                                    }else{
                                        return true;
                                    }
                                }else{
                                    return {
                                        valid: false,
                                        message: '请填写大于“年龄范围（男）最小值”的值'
                                    };
                                }
                            }
                        },
                        remote: {
                            type: "post",
                            delay: 500,
                            url: '${ctx}/testProject/validateAgeRange',
                            data: function (validator) {
                                return {
                                    minAge: $("#minAge").val(),
                                    maxAge: $("#maxAge").val(),
                                    testItemCode: $("#item").val(),
                                    exception: '${testProject.id}'
                                };
                            },
                            success:function(data){
                                alert();
                            },
                            message: '该年龄段已经存在参考值'
                        }
                    }
                },
                testMinValue: {
                    group: '.col-sm-4',
                    verbose: false,
                    validators: {
                        notEmpty: true,
                        regexp : {
                            regexp : /^[-+]?\d+(\.\d+)?$/,
                            message: "输入格式错误，请输入正确的数字"
                        },
                        callback: {
                            callback: function(value, validator) {
                                var testMaxValue = validator.getFieldElements('testMaxValue').val();
                                if(Number(testMaxValue)>=Number(value)){
                                    validator.updateStatus('testMaxValue', 'VALID');
                                    return true;
                                }else{
                                    return {
                                        valid: false,
                                        message: '请填写小于“参考值范围（男）最大值”的值'
                                    };
                                }
                            }
                        },
                    }
                },
                testMaxValue: {
                    group: '.col-sm-4',
                    verbose: false,
                    validators: {
                        notEmpty: true,
                        regexp : {
                            regexp : /^[-+]?\d+(\.\d+)?$/,
                            message: "输入格式错误，请输入正确的数字"
                        },
                        callback: {
                            callback: function(value, validator) {
                                var testMinValue = validator.getFieldElements('testMinValue').val();
                                if(Number(value)>=Number(testMinValue)){
                                    validator.updateStatus('testMinValue', 'VALID');
                                    return true;
                                }else{
                                    return {
                                        valid: false,
                                        message: '请填写大于“参考值范围（男）最小值”的值'
                                    };
                                }
                            }
                        },
                    }
                },
                minAgeGirl: {
                    group: '.col-sm-4',
                    verbose: false,
                    validators: {
                        notEmpty: true,
                        digits: true,
                        callback: {
                            callback: function(value, validator) {
                                var maxAgeGirl = validator.getFieldElements('maxAgeGirl').val();
                                if(Number(maxAgeGirl)>=Number(value)){
                                    validator.updateStatus('maxAgeGirl', 'VALID');
                                    //验证项目子项是否为选择
                                    validator.resetField("kind").validateField("kind");
                                    validator.resetField("testItemCode").validateField("testItemCode");
                                    if(!validator.isValidField("testItemCode")){
                                        return {
                                            valid: false,
                                            message: '项目子项不能为空'
                                        };
                                    }else{
                                        return true;
                                    }
                                }else{
                                    return {
                                        valid: false,
                                        message: '请填写小于“年龄范围（女）最大值”的值'
                                    };
                                }
                            }
                        },
                        remote: {
                            type: "post",
                            delay: 500,
                            url: '${ctx}/testProject/validateAgeGirlRange',
                            data: function (validator) {
                                return {
                                    minAgeGirl: $("#minAgeGirl").val(),
                                    maxAgeGirl: $("#maxAgeGirl").val(),
                                    testItemCode: $("#item").val(),
                                    exception: '${testProject.id}'
                                };
                            },
                            message: '该年龄段已经存在参考值'
                        },
                    }
                },
                maxAgeGirl: {
                    group: '.col-sm-4',
                    verbose: false,
                    validators: {
                        notEmpty: true,
                        digits: true,
                        callback: {
                            callback: function(value, validator) {
                                var minAgeGirl = validator.getFieldElements('minAgeGirl').val();
                                if(Number(value)>=Number(minAgeGirl)){
                                    validator.updateStatus('minAgeGirl', 'VALID');
                                    //验证项目子项是否为选择
                                    validator.resetField("kind").validateField("kind");
                                    validator.resetField("testItemCode").validateField("testItemCode");
                                    if(!validator.isValidField("testItemCode")){
                                        return {
                                            valid: false,
                                            message: '项目子项不能为空'
                                        };
                                    }else{
                                        return true;
                                    }
                                }else{
                                    return {
                                        valid: false,
                                        message: '请填写大于“年龄范围（女）最小值”的值'
                                    };
                                }
                            }
                        },
                        remote: {
                            type: "post",
                            delay: 500,
                            url: '${ctx}/testProject/validateAgeGirlRange',
                            data: function (validator) {
                                return {
                                    minAgeGirl: $("#minAgeGirl").val(),
                                    maxAgeGirl: $("#maxAgeGirl").val(),
                                    testItemCode: $("#item").val(),
                                    exception: '${testProject.id}'
                                };
                            },
                            message: '该年龄段已经存在参考值'
                        },
                    }
                },
                testMinValueGirl: {
                    group: '.col-sm-4',
                    verbose: false,
                    validators: {
                        notEmpty: true,
                        regexp : {
                            regexp : /^[-+]?\d+(\.\d+)?$/,
                            message: "输入格式错误，请输入正确的数字"
                        },
                        callback: {
                            callback: function(value, validator) {
                                var testMaxValueGirl = validator.getFieldElements('testMaxValueGirl').val();
                                if(Number(testMaxValueGirl)>=Number(value)){
                                    validator.updateStatus('testMaxValueGirl', 'VALID');
                                    return true;
                                }else{
                                    return {
                                        valid: false,
                                        message: '请填写小于“参考值范围（女）最大值”的值'
                                    };
                                }
                            }
                        },
                    }
                },
                testMaxValueGirl: {
                    group: '.col-sm-4',
                    verbose: false,
                    validators: {
                        notEmpty: true,
                        regexp : {
                            regexp : /^[-+]?\d+(\.\d+)?$/,
                            message: "输入格式错误，请输入正确的数字"
                        },
                        callback: {
                            callback: function(value, validator) {
                                var testMinValueGirl = validator.getFieldElements('testMinValueGirl').val();
                                if(Number(value)>=Number(testMinValueGirl)){
                                    validator.updateStatus('testMinValueGirl', 'VALID');
                                    return true;
                                }else{
                                    return {
                                        valid: false,
                                        message: '请填写大于“参考值范围（女）最小值”的值'
                                    };
                                }
                            }
                        },
                    }
                }
            }
        }).on('success.form.bv', function (e) {
            e.preventDefault();
            $.ajax({
                url:'${URL_ADM_TESTPROJECT_UPDATE}',
                type:'post',
                data:$('#myForm').serialize(),
                async : false, //默认为true 异步
                error:function(){
                    alert('error');
                },
                success:function(data){
                    window.parent.closeModal("update");
                }
            });
        });

        $("#closeModalButt").on("click",function(){
            window.parent.simpleCloseModal("update");
        });

    });

    <%--function doUpdate(){--%>
        <%--$.ajax({--%>
            <%--url:'${URL_ADM_TESTPROJECT_UPDATE}',--%>
            <%--type:'post',--%>
            <%--data:$('#myForm').serialize(),--%>
            <%--async : false, //默认为true 异步--%>
            <%--error:function(){--%>
                <%--alert('error');--%>
            <%--},--%>
            <%--success:function(data){--%>
                <%--window.parent.closeModal("update");--%>
            <%--}--%>
        <%--});--%>
    <%--}--%>

</script>
</body>
</html>